﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CodeFirst.Helps;

namespace CodeFirst
{
    class Program
    {
        //static void Main(string[] args)
        //{
        //    using (var context=new Context())
        //    {
        //        context.Database.CreateIfNotExists();
        //    }
        //    Console.WriteLine("Db has Created!");
        //    Console.Read();
        //}
        static void Main(string[] args)
        {
            #region 初始化器
            //Database.SetInitializer(new Initializer());
            Database.SetInitializer(new Initializer());
            #endregion
            //#region 创建表
            //using (var context = new Context())
            //{
            //    context.Database.CreateIfNotExists();
            //    var donators = new List<Donator>
            //    {
            //        new Donator
            //        {
            //            Name = "Ken",
            //            Amount = 50,
            //            DonateDate = new DateTime(2016,4,7)
            //        },
            //        new Donator
            //        {
            //            Name = "Liu",
            //            Amount = 80,
            //            DonateDate = new DateTime(2017,9,25)
            //        },
            //        new Donator
            //        {
            //            Name = "Fate",
            //            Amount = 99,
            //            DonateDate = new DateTime(2019,4,8)
            //        }
            //    };
            //    context.Donators.AddRange(donators);
            //    context.SaveChanges();
            //}
            //#endregion

            //Console.WriteLine("Db has Created!");
            //Console.WriteLine("Db has Finished!");
            //Console.Read();

            //#region 查询记录

            //using (var context = new Context())
            //{
            //    var donators = context.Donators;
            //    Console.WriteLine("Id\t\t姓名\t\t金额\t\t赞助日期");
            //    foreach (var donator in donators)
            //    {
            //        Console.WriteLine("{0}\t\t{1}\t\t{2}\t\t{3}", donator.DonatorId, donator.Name, donator.Amount, donator.DonateDate.ToShortTimeString());
            //    }
            //}

            //#endregion

            //#region 修改更新

            //using (var context = new Context())
            //{
            //    var donators = context.Donators;
            //    if (donators.Any())
            //    {
            //        var updateDonator = donators.First(d => d.Name == "Liu");
            //        updateDonator.Name = "LIU";
            //        context.SaveChanges();
            //    }
            //    Console.WriteLine("Id\t\t姓名\t\t金额\t\t赞助日期");
            //    foreach (var donator in donators)
            //    {
            //        Console.WriteLine("{0}\t\t{1}\t\t{2}\t\t{3}", donator.DonatorId, donator.Name, donator.Amount, donator.DonateDate.ToShortTimeString());
            //    }
            //}


            //#region 删除
            //using (var context = new Context())
            //{
            //    var donators = context.Donators;
            //    if (donators.Any())
            //    {
            //        var updateDonator = donators.Single(d => d.Name == "LIU");
            //        context.Donators.Remove(updateDonator);
            //        context.SaveChanges();
            //    }
            //}
            //#endregion
        }
    }
}
